﻿using System.Net.Sockets;
namespace CDIO
{
    namespace Util
    {
        public class Net
        {
            private string IP;
            private int Port;
            private TcpClient send;
            private TcpClient recv;
            private NetworkStream RecvStream;
            private TcpListener listener;
            private byte[] data;
            private bool Ready;
            public string BlackCardNo
            {
                get
                {
                    if (data!=null)
                    {
                        return System.Text.Encoding.Unicode.GetString(data);
                    }
                    return null;
                }
            }
            public Net(string IP = "127.0.0.1", int port = 4910)
            {
                this.IP = IP;
                Port = port;
                Ready = false;
            }

            public bool Send(string cardNo,bool suspend = true)
            {
                try
                {
                    send = new TcpClient(IP, Port);
                    NetworkStream stream = send.GetStream();
                    byte[] data = System.Text.Encoding.Unicode.GetBytes((suspend?"":"#")+cardNo);
                    stream.Write(data, 0, data.Length);
                    stream.Close();
                    send.Close();
                    return true;
                }
                catch (System.Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.Source + ":" + e.Message);
                }
                return false;
            }

            private void AfterRead(System.IAsyncResult IR)
            {
                try
                {
                    int recvCount = RecvStream.EndRead(IR);
                    if (recvCount>0)
                    {
                        string BlackCardNo = System.Text.Encoding.Unicode.GetString(data, 0, recvCount);
                        Ready = true;
                    }
                }
                catch (System.Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.Source + ":" + e.Message);
                }
            }

            private void AfterAccept(System.IAsyncResult IR)
            {
                recv = listener.EndAcceptTcpClient(IR);
                RecvStream = recv.GetStream();
                data = new byte[1024];
                RecvStream.BeginRead(data, 0, data.Length, AfterRead, null);
            }

            public void AsyRecvBlackList(System.AsyncCallback AfterRecv)
            {
                listener = new TcpListener(System.Net.IPAddress.Loopback,Port);
                listener.BeginAcceptTcpClient(AfterAccept, null);
            }

            public bool RecvBlackList(out string blackNo,out bool suspend)
            {
                bool result = false;
                blackNo = null;
                suspend = true;
                try
                {
                    listener = new TcpListener(System.Net.IPAddress.Loopback, Port);
                    listener.Start();
                    recv = listener.AcceptTcpClient();
                    RecvStream = recv.GetStream();
                    data = new byte[1024];
                    if (0 < RecvStream.Read(data, 0, data.Length))
                    {
                        blackNo = System.Text.Encoding.Unicode.GetString(data);
                        if (blackNo[0]=='#')
                        {
                            suspend = false;
                            blackNo = blackNo.Substring(1);
                        }
                        result = true;
                    }
                }
                catch (System.Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.Source + ":" + e.Message);
                    result = false;
                }
                return result;
            }
        }
    }
}